package com.sensePlusVR.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;

import com.sensePlusVR.activity.listener.AEditTextCallBack;

/**
 * 
 * @author: Loren
 * @create: 2017-9-13下午3:59:34
 * @desc: 主要为了封装处理一键删除所需要监听处理
 * 
 */
public class EditTextDeleteAll extends EditText implements TextWatcher,
		OnFocusChangeListener {
	private boolean focused;

	public EditTextDeleteAll(Context context) {
		super(context);
		init();
	}

	public EditTextDeleteAll(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public EditTextDeleteAll(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	private void init() {
		addTextChangedListener(this);
		setOnFocusChangeListener(this);
	}
	
	
	private AEditTextCallBack aEditTextCallBack;
	
	public void setOnAEditTextCallBack(AEditTextCallBack aEditTextCallBack){
		this.aEditTextCallBack = aEditTextCallBack;
		
	}
	
	

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {}

	@Override
	public void onTextChanged(CharSequence text, int start, int lengthBefore,
			int lengthAfter) {
		super.onTextChanged(text, start, lengthBefore, lengthAfter);
	}

	@Override
	public void afterTextChanged(Editable s) {
		if (focused) {
			int len = getText().length();
			
			boolean isVisibleDel = len > 0 ? true : false;
			boolean isVisibleBottomTip = (len < 6 || len >= 15) ? true : false;
			if(aEditTextCallBack != null){
				aEditTextCallBack.onDeleteVisible(EditTextDeleteAll.this, isVisibleDel);
				aEditTextCallBack.onBottomTipVisible(EditTextDeleteAll.this, isVisibleBottomTip);
			}
				

		}

	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		focused = hasFocus;
		boolean isVisibleDel = getText().length() > 0 && hasFocus ? true : false;
		boolean isVisibleBottomTip = hasFocus;
		if(aEditTextCallBack != null){
			aEditTextCallBack.onDeleteVisible(EditTextDeleteAll.this, isVisibleDel);
			aEditTextCallBack.onBottomTipVisible(EditTextDeleteAll.this, isVisibleBottomTip);
		}
	}

}
